<template>
  <div>
    <el-form
        :style='{"padding":"30px 40px","boxShadow":"0 0px 0px #999","borderRadius":"6px","flexWrap":"wrap","background":"none","display":"flex","justifyContent":"space-between"}'
        class="add-update-preview"
        ref="ruleForm"
        :model="ruleForm"
        label-width="150px"
    >
      <el-row>
        <!-- 患者       -->
        <el-form-item
            :style='{"padding":"0px","boxShadow":"0px 0px 0px 0px #d2b3aa","margin":"0 0 20px 0","borderColor":"#d3a7be","borderRadius":"0px","background":"#fff","borderWidth":"0px","display":"block","width":"100%","minWidth":"500px","borderStyle":"solid"}'
            v-if="flag=='yonghu'" label="用户名" prop="yonghuming">
          <el-input v-model="ruleForm.yonghuming" readonly placeholder="用户名" clearable></el-input>
        </el-form-item>
        <el-form-item
            :style='{"padding":"0px","boxShadow":"0px 0px 0px 0px #d2b3aa","margin":"0 0 20px 0","borderColor":"#d3a7be","borderRadius":"0px","background":"#fff","borderWidth":"0px","display":"block","width":"100%","minWidth":"500px","borderStyle":"solid"}'
            v-if="flag=='yonghu'" label="姓名" prop="xingming">
          <el-input v-model="ruleForm.xingming" placeholder="姓名" clearable></el-input>
        </el-form-item>
        <el-form-item
            :style='{"padding":"0px","boxShadow":"0px 0px 0px 0px #d2b3aa","margin":"0 0 20px 0","borderColor":"#d3a7be","borderRadius":"0px","background":"#fff","borderWidth":"0px","display":"block","width":"100%","minWidth":"500px","borderStyle":"solid"}'
            v-if="flag=='yonghu'" label="性别" prop="xingbie">
          <el-select v-model="ruleForm.xingbie" placeholder="请选择性别">
            <el-option
                v-for="(item,index) in yonghuxingbieOptions"
                v-bind:key="index"
                :label="item"
                :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
            :style='{"padding":"0px","boxShadow":"0px 0px 0px 0px #d2b3aa","margin":"0 0 20px 0","borderColor":"#d3a7be","borderRadius":"0px","background":"#fff","borderWidth":"0px","display":"block","width":"100%","minWidth":"500px","borderStyle":"solid"}'
            v-if="flag=='yonghu'" label="联系电话" prop="lianxidianhua">
          <el-input v-model="ruleForm.lianxidianhua" placeholder="联系电话" clearable></el-input>
        </el-form-item>
        <el-form-item
            :style='{"padding":"0px","boxShadow":"0px 0px 0px 0px #d2b3aa","margin":"0 0 20px 0","borderColor":"#d3a7be","borderRadius":"0px","background":"#fff","borderWidth":"0px","display":"block","width":"100%","minWidth":"500px","borderStyle":"solid"}'
            v-if="flag=='yonghu'" label="头像" prop="touxiang">
          <file-upload
              tip="点击上传头像"
              action="file/upload"
              :limit="3"
              :multiple="true"
              :fileUrls="ruleForm.touxiang?ruleForm.touxiang:''"
              @change="yonghutouxiangUploadChange"
          ></file-upload>
        </el-form-item>

        <!-- 医师       -->
        <el-form-item
            :style='{"padding":"0px","boxShadow":"0px 0px 0px 0px #d2b3aa","margin":"0 0 20px 0","borderColor":"#d3a7be","borderRadius":"0px","background":"#fff","borderWidth":"0px","display":"block","width":"100%","minWidth":"500px","borderStyle":"solid"}'
            v-if="flag=='yishi'" label="用户名" prop="yonghuming">
          <el-input v-model="ruleForm.yonghuming" readonly placeholder="用户名" clearable></el-input>
        </el-form-item>
        <el-form-item
            :style='{"padding":"0px","boxShadow":"0px 0px 0px 0px #d2b3aa","margin":"0 0 20px 0","borderColor":"#d3a7be","borderRadius":"0px","background":"#fff","borderWidth":"0px","display":"block","width":"100%","minWidth":"500px","borderStyle":"solid"}'
            v-if="flag=='yishi'" label="姓名" prop="xingming">
          <el-input v-model="ruleForm.xingming" placeholder="姓名" clearable></el-input>
        </el-form-item>
        <el-form-item
            :style='{"padding":"0px","boxShadow":"0px 0px 0px 0px #d2b3aa","margin":"0 0 20px 0","borderColor":"#d3a7be","borderRadius":"0px","background":"#fff","borderWidth":"0px","display":"block","width":"100%","minWidth":"500px","borderStyle":"solid"}'
            v-if="flag=='yishi'" label="性别" prop="xingbie">
          <el-select v-model="ruleForm.xingbie" placeholder="请选择性别">
            <el-option
                v-for="(item,index) in yonghuxingbieOptions"
                v-bind:key="index"
                :label="item"
                :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
            :style='{"padding":"0px","boxShadow":"0px 0px 0px 0px #d2b3aa","margin":"0 0 20px 0","borderColor":"#d3a7be","borderRadius":"0px","background":"#fff","borderWidth":"0px","display":"block","width":"100%","minWidth":"500px","borderStyle":"solid"}'
            v-if="flag=='yishi'" label="联系电话" prop="lianxidianhua">
          <el-input v-model="ruleForm.lianxidianhua" placeholder="联系电话" clearable></el-input>
        </el-form-item>
        <el-form-item
            :style='{"padding":"0px","boxShadow":"0px 0px 0px 0px #d2b3aa","margin":"0 0 20px 0","borderColor":"#d3a7be","borderRadius":"0px","background":"#fff","borderWidth":"0px","display":"block","width":"100%","minWidth":"500px","borderStyle":"solid"}'
            v-if="flag=='yishi'" label="头像" prop="touxiang">
          <file-upload
              tip="点击上传头像"
              action="file/upload"
              :limit="3"
              :multiple="true"
              :fileUrls="ruleForm.touxiang?ruleForm.touxiang:''"
              @change="yonghutouxiangUploadChange"
          ></file-upload>
        </el-form-item>

        <!--  管理员      -->
        <el-form-item
            :style='{"padding":"0px","boxShadow":"0px 0px 0px 0px #d2b3aa","margin":"0 0 20px 0","borderColor":"#d3a7be","borderRadius":"0px","background":"#fff","borderWidth":"0px","display":"block","width":"100%","minWidth":"500px","borderStyle":"solid"}'
            v-if="flag=='users'" label="用户名" prop="username">
          <el-input v-model="ruleForm.username" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item :style='{"width":"100%","padding":"0","margin":"0 auto","background":"none"}'>
          <el-button
              :style='{"border":"1px solid #6ebe9b","cursor":"pointer","padding":"0 16px","boxShadow":"inset 0px 0px 56px 0px #5dae8b","margin":"20px 40px 20px 0","color":"#fff","minWidth":"120px","outline":"none","borderRadius":"4px","background":"#fff","width":"auto","lineHeight":"44px","fontSize":"14px","height":"44px"}'
              type="primary" @click="onUpdateHandler">修 改
          </el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </div>
</template>
<script>
// 数字，邮件，手机，url，身份证校验
import {isNumber, isIntNumer, isEmail, isMobile, isPhone, isURL, checkIdCard} from "@/utils/validate";

export default {
  data() {
    return {
      ruleForm: {},
      flag: '',
      usersFlag: false,
      yonghuxingbieOptions: [],
    };
  },
  mounted() {
    var table = this.$storage.get("sessionTable");
    this.flag = table;
    this.$http({
      url: `${this.$storage.get("sessionTable")}/session`,
      method: "get"
    }).then(({data}) => {
      if (data && data.code === 0) {
        this.ruleForm = data.data;
      } else {
        this.$message.error(data.msg);
      }
    });
    this.yonghuxingbieOptions = "男,女".split(',')
  },
  methods: {
    yonghutouxiangUploadChange(fileUrls) {
      this.ruleForm.touxiang = fileUrls;
    },
    onUpdateHandler() {
      if ((!this.ruleForm.yonghuming) && 'yonghu' == this.flag) {
        this.$message.error('用户名不能为空');
        return
      }
      if ((!this.ruleForm.xingming) && 'yonghu' == this.flag) {
        this.$message.error('姓名不能为空');
        return
      }
      if ((!this.ruleForm.mima) && 'yonghu' == this.flag) {
        this.$message.error('密码不能为空');
        return
      }
      if ('yonghu' == this.flag && this.ruleForm.lianxidianhua && (!isMobile(this.ruleForm.lianxidianhua))) {
        this.$message.error(`联系电话应输入手机格式`);
        return
      }
      if (this.ruleForm.touxiang != null) {
        this.ruleForm.touxiang = this.ruleForm.touxiang.replace(new RegExp(this.$base.url, "g"), "");
      }
      if ('users' == this.flag && this.ruleForm.username.trim().length < 1) {
        this.$message.error(`用户名不能为空`);
        return
      }
      this.$http({
        url: `${this.$storage.get("sessionTable")}/update`,
        method: "post",
        data: this.ruleForm
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.$message({
            message: "修改信息成功",
            type: "success",
            duration: 1500,
            onClose: () => {
            }
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.el-date-editor.el-input {
  width: auto;
}

.add-update-preview .el-form-item /deep/ .el-form-item__label {
  padding: 0 10px 0 0;
  text-shadow: 0 1px 10px #fff;
  color: #666;
  background: none;
  display: inline-block;
  width: 150px;
  font-size: 14px;
  line-height: 40px;
  text-align: right;
}

.add-update-preview .el-form-item /deep/ .el-form-item__content {
  margin-left: 150px;
}

.add-update-preview .el-input /deep/ .el-input__inner {
  border: 1px solid #c7ebdb;
  border-radius: 4px;
  padding: 0 12px;
  box-shadow: inset 0px 0px 24px 0px #e8f9f2;
  outline: none;
  color: #666;
  background: #fff;
  display: inline-block;
  width: auto;
  font-size: 14px;
  min-width: 300px;
  height: 40px;
}

.add-update-preview .el-select /deep/ .el-input__inner {
  border: 1px solid #c7ebdb;
  border-radius: 4px;
  padding: 0 10px;
  box-shadow: inset 0px 0px 24px 0px #e8f9f2;
  outline: none;
  color: #666;
  background: #fff;
  width: auto;
  font-size: 14px;
  min-width: 300px;
  height: 40px;
}

.add-update-preview .el-date-editor /deep/ .el-input__inner {
  border: 1px solid #c7ebdb;
  border-radius: 4px;
  padding: 0 10px 0 30px;
  box-shadow: inset 0px 0px 24px 0px #e8f9f2;
  outline: none;
  color: #666;
  background: #fff;
  width: auto;
  font-size: 14px;
  min-width: 300px;
  height: 40px;
}

.add-update-preview /deep/ .el-upload--picture-card {
  background: transparent;
  border: 0;
  border-radius: 0;
  width: auto;
  height: auto;
  line-height: initial;
  vertical-align: middle;
}

.add-update-preview /deep/ .el-upload-list .el-upload-list__item {
  border: 1px solid #c7ebdb;
  cursor: pointer;
  border-radius: 4px;
  box-shadow: inset 0px 0px 24px 0px #e8f9f2;
  color: #aaa;
  background: #fff;
  width: 200px;
  font-size: 32px;
  line-height: 100px;
  text-align: center;
  height: 100px;
}

.add-update-preview /deep/ .el-upload .el-icon-plus {
  border: 1px solid #c7ebdb;
  cursor: pointer;
  border-radius: 4px;
  box-shadow: inset 0px 0px 24px 0px #e8f9f2;
  color: #aaa;
  background: #fff;
  width: 200px;
  font-size: 32px;
  line-height: 100px;
  text-align: center;
  height: 100px;
}

.add-update-preview .el-textarea /deep/ .el-textarea__inner {
  border: 1px solid #c7ebdb;
  border-radius: 4px;
  padding: 12px;
  box-shadow: inset 0px 0px 24px 0px #e8f9f2;
  outline: none;
  color: #666;
  background: #fff;
  width: auto;
  font-size: 14px;
  min-height: 150px;
  min-width: 640px;
  height: auto;
}
</style>
